package com.alen.sun.dao;

import com.alen.sun.bean.CheckIn;
import com.alen.sun.bean.Folk;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FolkMapper {
    //添加家属
    @Insert("INSERT INTO folk (phoneNum, password, name, gender, status, elderlyId) values "+
            " (#{phoneNum}, #{password}, #{name}, #{gender}, #{status}, #{elderlyId})")
    void addFolk(Folk folk);

    //修改家属
    @Update("UPDATE folk SET  password = #{password}, name = #{name}, gender = #{gender}, "+
            "status = #{status}, elderlyId = #{elderlyId} where phoneNum = #{phoneNum}")
    Boolean updateFolk(Folk folk);

    //删除家属
    @Delete("DELETE FROM folk where phoneNum = #{phoneNum}")
    Boolean deleteFolk(Folk folk);

    //查找家属-根据手机号查找家属,有记录就不不能注册该手机号了
    @Select("SELECT COUNT(*) FROM folk WHERE phoneNum = #{phoneNum}")
    Integer selectFolk(Folk folk);

    //根据phoneNum查找记录
    @Select("SELECT * FROM folk WHERE phoneNum = #{phoneNum}")
    Folk selectPhoneNum(Folk folk);


    /**
     * 批量删除家属
     * @param ids
     */
    @Delete({
            "<script>",
            "delete from folk where phoneNum in",
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'>#{id}</foreach>",
            "</script>"
    })
    Boolean deleteByIds(List<String> ids);


    /**
     * 查询总记录数
     * @return
     */
    @Select("select count(*) from folk")
    Long count();

    /**
     * 分页查询,获取列表数据
     * @param start
     * @param pageSize
     * @return
     */
    @Select("select * from folk limit #{start},#{pageSize}")
    List<Folk> page(Integer start, Integer pageSize);

    //获取家属个人信息
    @Select("SELECT * FROM folk WHERE phoneNum = #{PhoneNum}")
    Folk getPersonalFolkInfo(String PhoneNum);

    //网页搜索功能 聚合模糊查询, 姓名,老人电话号码,家属电话号码
    @Select("SELECT * FROM folk " +
            "WHERE phoneNum LIKE CONCAT('%', #{keywords}, '%') " +
            "OR name LIKE CONCAT('%', #{keywords}, '%')")
    List<Folk> searchByKeywords(String keywords);
}
